class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> mp;
        vector<int> res;
        for (auto x : nums1) ++mp[x];
        for (auto x : nums2) {
            if (mp[x]) {
                res.push_back(x);
                --mp[x];
            }
        }
        return res;
    }
};
